<?php
/**
 * index.php     Zhuayi 腾讯微博操作类
 *
 * @copyright    (C) 2005 - 2010  Zhuayi
 * @licenes      http://www.zhuayi.net
 * @lastmodify   2010-10-27
 * @author       zhuayi
 * @QQ			 2179942
 */

class appqq extends snoopy
{
	var $app_key;
	
	var $app_secret;
	
	var $oauth_token='';
	
	var $oauth_token_secret='';
	
	var $oauth_version = '1.0';
	
	function __construct()
	{
		$this->curl_path = ZHUAYI_ROOT.'/statics/curl/curl.exe';
	}
	
	
	/* 默认参数 */
	function _default()
	{
		$array['oauth_consumer_key'] = $this->app_key;
		$array['oauth_nonce'] = $this->oauth_nonce();
		$array['oauth_signature_method'] = 'HMAC-SHA1';
		$array['oauth_timestamp'] = time();
		if (!empty($this->oauth_token))
		$array['oauth_token'] = $this->oauth_token;
		$array['oauth_version'] = $this->oauth_version;
		return $array;
	}
	
	
	
	
	/* http 操作 */
	function http($url , $array = array(), $method='GET',$token='',$file='')
	{
		
		$default = $this->_default();
		if (!empty($array))
		$array = array_merge($default,$array);
		else
		$array = $default;
		
		/* 排序数组 */
		$keys = array_keys($array);
		$values = array_values($array);
		
		uksort($array, 'strcmp');
		
		$array['oauth_signature'] = $this->singn($url,$array,$method,$token);
		
		
		
		if ($method == 'GET')
		{
			foreach ($array as $key=>$val)
			{
				$f[] = $key.'='.urlencode($val);
			}

			$url .= '?'.implode('&',$f);
			
			$this->fetch($url);
		}
		else
		{
			$array['oauth_signature'] = $this->urlencode_rfc3986($array['oauth_signature']);
			foreach ($array as $key=>$val)
			{
				
				$array2[$key] = urldecode(trim($val));
			}
			$array = $array2;
			unset($array2);
			
			if (!empty($file))
			{
				$formfiles[$file[0]] = $file[1];
				$this->_submit_type = "multipart/form-data"; 
			}

			$this->submit($url,$array,$formfiles);
		}

		return $this->reset();
	}
	
	/*
	 * 随机串（32个字符长度）随机生成
	 *
	 */
	function oauth_nonce()
	{
		return md5(mt_rand().time());
	}
	
	/* HMAC 签名算法 */
	function singn($url,$array,$method='GET',$token)
	{
		foreach ($array as $key=>$val)
		{
			$base_string[] = urldecode($key).'%3D'.$this->urlencode_rfc3986($this->urlencode_rfc3986(trim($val)));
		}
		
		$base_string = implode('%26',$base_string);
		//$this->test($base_string);
		$base_string =  $method.'&'.rawurlencode($url).'&'.$base_string;
		
		
		return  base64_encode(hash_hmac('SHA1', $base_string, $this->app_secret.'&'.$token, true));
	}
	
	/* 获取request token */
	function request_token($oauth_callback = NULL)
	{		
		return $this->http('https://open.t.qq.com/cgi-bin/request_token',array('oauth_callback'=>$oauth_callback));
	}
	

	
	/* 获取access token 和 授权码*/
	function access_token($oauth_verifier)
	{
		//echo $oauth_token;
		return $this->http('https://open.t.qq.com/cgi-bin/access_token',array('oauth_verifier'=>$oauth_verifier),'GET',$this->oauth_token_secret);
	}
	
	/* 获取自己的详细资料 */
	function user_info()
	{
		return $this->http('http://open.t.qq.com/api/user/info',array('format'=>'json'),'GET',$this->oauth_token_secret);
	}
	
	/* 获取微博 */
	function weibo_list($array)
	{
		//echo $this->oauth_token_secret;
		$array['format'] = 'json';
		$reset = $this->http('http://open.t.qq.com/api/statuses/home_timeline',$array,'GET',$this->oauth_token_secret);
		$reset = $reset['data']['info'];
		return $reset;
	}
	
	/* 转发一条微博 */
	function weibo_repost($id,$content,$ip='127.0.0.1')
	{
		$array['format'] = 'json';
		$array['content'] = $content;
		$array['reid'] = $id;
		$array['clientip'] = $ip;
		
		$reset = $this->http('http://open.t.qq.com/api/t/re_add',$array,'POST',$this->oauth_token_secret);
		//$reset = $reset['data']['id'];
		return $reset;
	}
	
	/* 评论一条信息 */
	function comment($id,$content,$ip='127.0.0.1')
	{
		$array['format'] = 'json';
		$array['content'] = $content;
		$array['reid'] = $id;
		$array['clientip'] = $ip;
		
		$reset = $this->http('http://open.t.qq.com/api/t/comment',$array,'POST',$this->oauth_token_secret);
		//$reset = $reset['data']['id'];
		return $reset;
	}
	/* 获取一条微博数据 */
	function weibo($id)
	{
		$array['format'] = 'json';
		$array['id'] = $id;
		
		$reset = $this->http('http://open.t.qq.com/api/t/show',$array,'GET',$this->oauth_token_secret);
		$reset = $reset['data'];
		return $reset;
	}
	
	/* 获取单条微博的转发或点评列表 */
	function re_list($array)
	{
		$array['format'] = 'json';
		
		$reset = $this->http('http://open.t.qq.com/api/t/re_list',$array,'GET',$this->oauth_token_secret);
		$reset = $reset['data']['info'];
		return $reset;
	}
	
	/* 转播数或点评数 */
	function re_count($ids)
	{
		$array['format'] = 'json';
		$array['ids'] = $ids;
		$array['flag'] = '0';
		
		$reset = $this->http('http://open.t.qq.com/api/t/re_count',$array,'GET',$this->oauth_token_secret);
		return $reset;
	}
	
	/* 发布一条微博 */
	function weibo_info($content,$file)
	{
		$status = str_replace('+','',$status);
		
		$array['format'] = 'json';
		$array['content'] = $content;
		
		$array['clientip'] = $this->get_ip();
		
		if (!empty($file[1]))
			$reset =  $this->http('http://open.t.qq.com/api/t/add_pic',$array,'POST',$this->oauth_token_secret,$file);
		else
			$reset =  $this->http('http://open.t.qq.com/api/t/add',$array,'POST',$this->oauth_token_secret);
		
		return $reset;
	}
	
	/* 删除一条微博 */
	function delete($id)
	{
		$array['format'] = 'json';
		$array['id'] = $id;
		$reset = $this->http('http://open.t.qq.com/api/t/del',$array,'POST',$this->oauth_token_secret);
		return $reset;
	}
	
	/* @atme */
	function atme($array)
	{
		$array['format'] = 'json';
		$reset = $this->http('http://open.t.qq.com/api/statuses/mentions_timeline',$array,'GET',$this->oauth_token_secret);
		
		return $reset['data']['info'];
	}
	
	/* 我的收藏 */
	function favorites()
	{
		$array['format'] = 'json';
		$reset =  $this->http('http://open.t.qq.com/api/fav/list_t',$array,'GET',$this->oauth_token_secret);
		return $reset['data']['info'];
	}
	
	/* 添加一个收藏 */
	function add_favorites($id)
	{
		$array['format'] = 'json';
		$array['id'] = $id;
		$reset =  $this->http('http://open.t.qq.com/api/fav/addt',$array,'POST',$this->oauth_token_secret);
		return $reset;
	}
	
	/* 删除一个收藏 */
	function del_favorites($id)
	{
		$array['format'] = 'json';
		$array['id'] = $id;
		$reset =  $this->http('http://open.t.qq.com/api/fav/delt',$array,'POST',$this->oauth_token_secret);
		return $reset;
	}
	
	/* 获取我的微博 */
	function my_weibo()
	{
		$array['format'] = 'json';
		$reset = $this->http('http://open.t.qq.com/api/statuses/broadcast_timeline',$array,'GET',$this->oauth_token_secret);
		$reset = $reset['data']['info'];
		return $reset;
	}
	
	/* 返回值鉴定 */
	function reset()
	{
		
		if (empty($this->results))
		{
			return array('error_code'=>'-1','error_cn'=>'未知错误');
		}
		elseif (trim($this->response_code) =='HTTP/1.1 200 OK')
		{
			return strings::json_dearray($this->results);
			
		}


	}
	
	function test($array)
	{
		
		echo '<pre>';
		echo '<br>===================================================================<br>';
		print_r($array);
		echo '<br>===================================================================<br>';
		
	}
	
	function urlencode_rfc3986($input) { 
        if (is_array($input)) { 
            return array_map(array('outh', 'urlencode_rfc3986'), $input); 
        } else if (is_scalar($input)) { 
            return str_replace( 
                '+', 
                ' ', 
                str_replace('%7E', '~', rawurlencode($input)) 
            ); 
        } else { 
            return ''; 
        } 
    }
	
	function get_ip()
	{

		if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
		$ip = getenv("HTTP_CLIENT_IP");
		else
		if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
		$ip = getenv("HTTP_X_FORWARDED_FOR");
		else
		if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
		$ip = getenv("REMOTE_ADDR");
		else
		if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
		$ip = $_SERVER['REMOTE_ADDR'];
		else
		$ip = "unknown";
		return ($ip);
	}

	

}





?>